package com.atguigu.sparearray;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * @author Amos
 * @date 2022/5/27
 */

public class SpareArray {
    public static void main(String[] args) throws IOException {
        //创建一个原始的二维数组
        // 0：表示没有棋子，1表示 黑子 2表示蓝子
        int chessArr1[][] = new int[11][11];
        System.out.println(chessArr1.length);
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][5] = 2;
        // 输出原始的二维数组
        System.out.println("原始的二维数组~~");
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        // 将二维数组 转 稀疏数组
        // 1. 先遍历二维数组 得到非0数据的个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }
        // 2. 创建稀疏数组
        int spareArr[][] = new int[sum + 1][3];
        // 3. 数组赋值
        spareArr[0][0] = 11;
        spareArr[0][1] = 11;
        spareArr[0][2] = sum;

        // 4. 遍历原始数组，将数据放入到稀疏数组
        int count = 1;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    spareArr[count][0] = i;
                    spareArr[count][1] = j;
                    spareArr[count][2] = chessArr1[i][j];
                    count++;
                }
            }
        }
        System.out.println("得到稀疏矩阵为：");
        for (int i = 0; i < spareArr.length; i++) {
            System.out.printf("%d\t%d\t%d\t\n",spareArr[i][0],spareArr[i][1],spareArr[i][2]);
        }
        System.out.println("存储稀疏矩阵到本地：");
        writeFile("C:/DataStructure/map.data", spareArr);
        System.out.println("从本地读取稀疏数组，恢复成原始数组：");
        System.out.println("###通过函数getSparseArrayByReadFile从本地读取spare数组###");
        int[][] sparseArrayByReadFile = getSparseArrayByReadFile("C:/DataStructure/map.data");
        for (int[] ints : sparseArrayByReadFile) {
            System.out.printf("%d\t%d\t%d\t",ints[0],ints[1],ints[2]);
            System.out.println();
        }
        System.out.println("#################");

        // 将稀疏数组恢复成原始数组
        int[][] chessArr2 = new int[spareArr[0][0]][spareArr[0][1]];
        for (int i = 1; i < spareArr.length; i++) {
            chessArr2[spareArr[i][0]][spareArr[i][1]] = spareArr[i][2];
        }
        System.out.println("恢复后的原始数组为：");
        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
    }

    public static void writeFile(String filePath,int[][] arry) throws IOException {
        System.out.println("===================将数组写入文件====================");
        Path path = Paths.get(filePath);
        StringBuffer content = new StringBuffer();
        for (int[] ints : arry) {
            for (int item : ints) {
                content.append(item+" ");
            }
            content.append("\n");
        }

//        Files.write(path, content.toString().getBytes("UTF-8"), StandardOpenOption.APPEND, StandardOpenOption.WRITE);
        Files.write(path, content.toString().getBytes("UTF-8"));

    }

    public static int[][]  getSparseArrayByReadFile(String filePath) throws IOException {
        System.out.println("===================从文件中读取数组====================");
        List<String> list = Files.readAllLines(Paths.get(filePath));
        int size = list.size();
        int[][] newSparseArray = new int[size][3];
        String[] split = null;
        for (int i = 0; i < size; i++) {
            split = list.get(i).split(" ");
            for (int j = 0; j < split.length; j++) {
                newSparseArray[i][j] = Integer.valueOf(split[j]);
            }
        }
        return newSparseArray;
    }

}
